﻿
namespace Anderson.NetLobby.Common
{
    public class NetMessage<T>
    {
        public uint MessageLength = 0;
        public uint MessageBOF = 0;
        public uint MessageLevel1 = 0;
        public uint MessageLevel2 = 0;
        public uint MessageLevel3 = 0;
        public long RoomId = 0;
        public T MessageContent;
        public uint MessageEOF = 0;

        public NetMessage()
        {
            MessageBOF = NetLobbyDefination.NET_MESSAGE_BOF;
            MessageEOF = NetLobbyDefination.NET_MESSAGE_EOF;
        }

        public NetMessage(long rId, uint one, uint two, uint three, T content) : this()
        {
            RoomId = rId;

            MessageLevel1 = one;
            MessageLevel2 = two;
            MessageLevel3 = three;

            MessageContent = content;

            CalculateLength();
        }

        public int CalculateLength()
        {
            byte[] bytesOfContent = ObjectSerialize.Serialize(MessageContent);

            int sizeOfUInt32 = sizeof(uint);
            int sizeOfLong = sizeof(long);
            int size = sizeOfUInt32 * 5 + sizeOfLong;//不包含MessageLength

            size += bytesOfContent.Length;

            MessageLength = (uint)size;

            return size;
        }
    }
}
